const Controller = require('egg').Controller;

class UserController extends Controller {
    async index() {
        try {
            const userList = await this.ctx.service.user.getUserList();
            this.ctx.body = {
                code: 2000,
                message: true,
                data: userList
            }
        } catch (error) {
            this.ctx.body = {
                code:3000,
                message:false
            }
        }

    }
    // 登录
    async create() {
        try {
            // 获取到传递过来的请求数据
            let username = this.ctx.request.body.username;
            let password = this.ctx.request.body.password;
            
            // 让服务层的user的Login方法进行处理
            const token = await this.ctx.service.user.Login(username,password);
            // 响应
            this.ctx.body = {
                code: 2000,
                message: true,
                data: token
            }
        } catch (error) {
            this.ctx.body = {
                code:3000,
                message:false
            }
        }

    }
}

module.exports = UserController;
